package com.zhuang.chapter18;

import java.util.Arrays;

public class Test2 {

	public static void main(String[] args) {
		String s = "abc";
		// test01();
		// array();
		test02();

	}
	// System.out.println(method01(s)); method02(s); }

	public static boolean method01(String s) {
		char[] charArray = s.toCharArray();
		boolean flag = true;
		for (int i = 0; i < charArray.length; i++) {
			System.out.println(charArray[i]);
			if (charArray[i] >= 'a' && charArray[i] <= 'z' || charArray[i] >= 'A' && charArray[i] <= 'Z') {
				flag = true;
			} else {
				flag = false;
				break;
			}
		}
		return flag;
	}

	public static boolean method02(String s) {
		boolean find = s.matches("[a-zA-Z]+");
		System.out.println(find);
		return find;
	}

	// 杨辉三角例子
	public static void test01() {
		int r = 5;

		// Declaring 2-D array with 5 rows
		int arr[][] = new int[r][];

		// Creating a 2D array such that first row
		// has 1 element, second row has two
		// elements and so on.
		for (int i = 0; i < arr.length; i++)
			arr[i] = new int[i + 1];

		// Initializing array
		int count = 0;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] = count++;
			}
		}

		// Displaying the values of 2D Jagged array
		System.out.println("Contents of 2D Jagged Array");
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++)
				System.out.print(arr[i][j] + " ");
			System.out.println();
		}
	}

	public static void array() {
		int[][] arr = new int[3][4];
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[0].length; j++) {
				arr[i][j] = i;
			}
			System.out.println(Arrays.toString(arr[i]));
		}
	}

	// 对角线的和
	public static void test02() {
		int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[0].length; j++) {
				// 主对角线
				if (i == j) {
					sum += arr[i][j];
				}
				// 次对角线
				if (i + j == arr.length - 1) {
					sum += arr[i][j];
				}
			}
			// 00 01 02 10 11 12 20 21 22
			System.out.println(Arrays.toString(arr[i]));
		}
		System.out.println("对角线的和为" + sum);
	}

}
